# avoid some cmake warnings
IF(POLICY CMP0026)
 CMAKE_POLICY(SET CMP0026 OLD)
ENDIF()

INCLUDE_DIRECTORIES(${LUA_INCDIR})
IF(LUALIB)
  LINK_DIRECTORIES(${LUA_LIBDIR}) # note: must be done before defining target
ENDIF()

ADD_LIBRARY(luaT SHARED luaT.h luaT.c)

IF (BUILD_STATIC OR "$ENV{STATIC_TH}" STREQUAL "YES")
  ADD_LIBRARY(luaT_static STATIC luaT.h luaT.c)
  SET_TARGET_PROPERTIES(luaT_static PROPERTIES
    COMPILE_FLAGS "-fPIC")
  SET_TARGET_PROPERTIES(luaT_static PROPERTIES
    PREFIX "lib" IMPORT_PREFIX "lib" OUTPUT_NAME "luaT")
ENDIF()

SET_TARGET_PROPERTIES(luaT PROPERTIES
  VERSION   0
  SOVERSION 0)

IF(APPLE)
  SET_TARGET_PROPERTIES(luaT PROPERTIES
    LINK_FLAGS "-undefined dynamic_lookup")
ENDIF()

IF(LUALIB)
  TARGET_LINK_LIBRARIES(luaT ${LUALIB}) # must be done after ;)
ENDIF()

INSTALL(TARGETS luaT
  EXPORT torch-exports
  RUNTIME DESTINATION "${Torch_INSTALL_BIN_SUBDIR}"
  LIBRARY DESTINATION "${Torch_INSTALL_LIB_SUBDIR}"
  ARCHIVE DESTINATION "${Torch_INSTALL_LIB_SUBDIR}")

INSTALL(FILES luaT.h
          DESTINATION "${Torch_INSTALL_INCLUDE_SUBDIR}")

# Create luaT.cmake
GET_TARGET_PROPERTY(LUAT_OUTPUT_NAME luaT LOCATION)
GET_FILENAME_COMPONENT(LUAT_OUTPUT_NAME ${LUAT_OUTPUT_NAME} NAME)
SET(LUAT_LIBRARIES "${Torch_INSTALL_LIB}/${LUAT_OUTPUT_NAME}")
SET(LUAT_INCLUDE_DIR "${Torch_INSTALL_INCLUDE}")
CONFIGURE_FILE(luaTConfig.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/cmake-exports/luaTConfig.cmake")
INSTALL(FILES "${CMAKE_CURRENT_BINARY_DIR}/cmake-exports/luaTConfig.cmake"
  DESTINATION "${Torch_INSTALL_CMAKE_SUBDIR}")
